package cn.yunhe.servlet;

import cn.yunhe.dao.IAdminDao;
import cn.yunhe.dao.impl.AdminDao;
import cn.yunhe.entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //判断用户是否存在
        IAdminDao adminDao = new AdminDao();
        Admin admin = adminDao.login(username, password);
        if (admin != null) {
            HttpSession session = request.getSession();
            session.setAttribute("admin", admin);
            request.getRequestDispatcher("LiuYanListServlet").forward(request, response);
        } else {
            PrintWriter out = response.getWriter();
            out.print("<script>alert('用户名或密码输入错误,请重新输入!!!');location.href='login.html'</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
